#pragma once

#include <MyControls/SegmentSet.h>
#include <MyControls/SegmentSetCmdImpl.h>
#include "CXSegment.h"
#include "MasksCollection.h"

#include <vector>

using My::Size;

class CXComp;

class CXSegmentSet : 
	public My::SegmentSetCmdImpl,
	public My::TypedSegmentSet<CXSegment>,
	public My::EventListener
{
	DECLARE_MESSAGE_MAP()
public:
	CXSegmentSet(CXComp &);
	~CXSegmentSet();
	void Clear();
	xml::stream & Serialize(xml::stream & s);
	bool AreThereSegmentsToProcess() const;
public:
	virtual My::Doc & GetDoc();
	virtual bool IsEmpty() const;
	virtual Size GetSegmentCount() const;
	virtual CXSegment & GetSegment(Size);
	virtual void MoveSelection(int);
public:
	const CXComp & GetComp() const
		{ return m_Comp; }
	CXComp & GetComp() 
		{ return m_Comp; }
	CXSegment * GetActiveSegmentPtr();
	CXSegment * GetTopmostSegmentPtr(Size Frame) const;
	/// returns topmost segment for the frame with MaskMode not equal to None
	CXSegment * GetTopmostNotNoneSegmentPtr(Size Frame) const;
	bool IsSomewhereTopmost(Size SegmentIndex) const;
	/// lists all masks with not-None mask mode
	void ListMasksWithCombMode(Size Frame, bool SmoothMask, MasksCollection & List) const;
public: //EventListener interface
	virtual void ProcessEvent(My::Event &);
public: //My::SegmentSetCmdImpl interface
	virtual My::Sequence * GetSequence();
	virtual My::SegmentSet * GetSegmentSet() { return this; }
protected:
	virtual void Insert(Size, CXSegment *);
	virtual CXSegment * Erase(Size);
private:
	CXComp &					m_Comp;
	std::vector<CXSegment *>	m_Segments;
public:
	virtual BOOL OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo);
	afx_msg void OnSegmentNew();
	afx_msg void OnUpdateSegmentNew(CCmdUI *pCmdUI);
	afx_msg void OnSegmentNewFromActive();
	afx_msg void OnUpdateSegmentNewFromActive(CCmdUI *pCmdUI);
	afx_msg void OnSegmentDelete();
	afx_msg void OnUpdateSegmentDelete(CCmdUI *pCmdUI);
	afx_msg void OnSegmentCancelAll();
	afx_msg void OnUpdateSegmentCancelAll(CCmdUI *pCmdUI);
	afx_msg void OnSegmentProcessAll();
	afx_msg void OnUpdateSegmentProcessAll(CCmdUI *pCmdUI);
};

inline xml::stream & operator || (xml::stream & s, CXSegmentSet & g)
	{ return g.Serialize(s); }
